package com.daxidi.cn.config.wxconfig;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author jialin
 * @createTime 2020-09-15
 */
@Component
@Data
@ConfigurationProperties(prefix = "wx.login")
public class WxConfig {
    private String appid;
    private String redirectUri;
    private String codeUri;
    private String appsecret;
    private String accessTokenUri;
    private String userInfoUri;
    private String refreshTokenUri;
    private String openIdTokenUri;
    private String appidClerk;
    private String appsecretClerk;

    public String getCode(){
        StringBuffer stringBuffer = new StringBuffer(getCodeUri());
        stringBuffer.append("appid=").append(getAppid());
        stringBuffer.append("&").append("redirect_uri=").append(getRedirectUri());
        stringBuffer.append("&").append("&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect");
        return stringBuffer.toString();
    }
    
    public String getAccessToken(String code){
        StringBuffer stringBuffer = new StringBuffer(getAccessTokenUri());
        stringBuffer.append("?appid=").append(getAppid());
        stringBuffer.append("&").append("secret=").append(getAppsecret());
        stringBuffer.append("&").append("code=").append(code);
        stringBuffer.append("&grant_type=authorization_code");
        return stringBuffer.toString();
    }
    
    public String getUserInfo(String accessToken,String openId){
        StringBuffer stringBuffer = new StringBuffer(getUserInfoUri());
        stringBuffer.append("access_token=").append(accessToken);
        stringBuffer.append("&").append("openid=").append(openId);
        stringBuffer.append("&lang=zh_CN");
        return stringBuffer.toString();
    }
    
    public String getRefreshToken(String appid,String refreshToken){
        StringBuffer stringBuffer = new StringBuffer(getRefreshTokenUri());
        stringBuffer.append("appid=").append(appid);
        stringBuffer.append("&grant_type=refresh_token");
        stringBuffer.append("&").append("refresh_token=").append(refreshToken);
        return stringBuffer.toString();
    }

    public String getOpenId(String appid, String code){
        StringBuffer stringBuffer = new StringBuffer(getOpenIdTokenUri());
        stringBuffer.append("appid=").append(appid);
        stringBuffer.append("&secret=").append(getAppsecret());
        stringBuffer.append("&js_code=").append(code);
        stringBuffer.append("&").append("grant_type=authorization_code");
        return stringBuffer.toString();
    }

    public String getClerkOpenId(String appid, String code){
        StringBuffer stringBuffer = new StringBuffer(getOpenIdTokenUri());
        stringBuffer.append("appid=").append(appidClerk);
        stringBuffer.append("&secret=").append(getAppsecretClerk());
        stringBuffer.append("&js_code=").append(code);
        stringBuffer.append("&").append("grant_type=authorization_code");
        return stringBuffer.toString();
    }
    
}
